﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Assigment_7
{
    public partial class Form1 : Form
    {
        SqlConnection con;

        public Form1()
        {
            InitializeComponent();
        }

        private void DisableCaption()
        {
            //txtBillNo.Enabled = false;
            //txtProName.Enabled = false;
            //txtDesc.Enabled = false;
            //txtQuanti.Enabled = false;
            //txtPrice.Enabled = false;
            //txtAmount.Enabled = false;

            //btnSearch.Enabled = false;
            btnUpdate.Enabled = false;
            btnPrint.Enabled = false;
            //btnClose.Enabled = false;
        }

        private void EnableCaption()
        {
            btnUpdate.Enabled = true;
            btnPrint.Enabled = true;
        }

        private void ResetForm()
        {
            DisableCaption();
            txtProName.Text = "";
            txtDesc.Text = "";
            txtQuanti.Text = "" + 0;
            txtPrice.Text = "" + 0;
            txtAmount.Text = "" + 0;
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DisableCaption();
        }

        private void txtBillNo_MouseClick(object sender, MouseEventArgs e)
        {
            txtBillNo.SelectAll();
            if (txtBillNo.Text == "(Enter Bill No to Search or Update)")
            {
                txtBillNo.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                txtBillNo.Text = "";
            }
        }

        private void txtBillNo_Leave(object sender, EventArgs e)
        {
            if (txtBillNo.Text == "")
            {
                txtBillNo.Font = new System.Drawing.Font("Segoe UI Light", 9.75F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                txtBillNo.Text = "(Enter Bill No to Search or Update)";
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            // Check empty field
            if ((txtBillNo.Text.Trim() == "") || (txtBillNo.Text == "(Enter Bill No to Search or Update)"))
            {
                lblCheckBill.Text = "Please enter Bill No";
                lblCheckBill.Visible = true;
                return;
            }
            lblCheckBill.Visible = false;
            string BillNo = txtBillNo.Text.Trim();
            try
            {
                con = new SqlConnection(Properties.Resources.sCN);
                con.Open();

                String query = "SELECT * FROM tbOrderBill WHERE BillNo = @1";
                SqlCommand cmd = new SqlCommand(query, con);

                cmd.Parameters.AddWithValue("@1", BillNo);

                SqlDataReader sdr = cmd.ExecuteReader();

                if (sdr.Read())
                {
                    txtProName.Text = sdr[1].ToString();
                    txtDesc.Text = sdr[2].ToString();
                    txtQuanti.Text = sdr[3].ToString();
                    txtPrice.Text = sdr[4].ToString();
                    int amount = int.Parse(txtQuanti.Text) * int.Parse(txtPrice.Text);
                    txtAmount.Text = "" + amount;

                    EnableCaption(); // Cho hien nut Update va Print
                }
                else
                {
                    if (MessageBox.Show("Not Found!!!") == DialogResult.OK)
                    {
                        txtBillNo.SelectAll();
                        txtBillNo.Focus();
                    }
                    ResetForm();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }
            



        }

        private void txtBillNo_MouseEnter(object sender, EventArgs e)
        {
            
        }

        private void txtBillNo_Enter(object sender, EventArgs e)
        {
            
        }

        private void txtBillNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13) // 13 là code của phím Enter
            {
                btnSearch_Click(sender, e);
            }
        }

        private void pDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            string data = "";
            data += "ORDER DETAILS";
            data += "\nBill No: " + txtBillNo.Text;
            data += "\nProduct Name: " + txtProName.Text;
            data += "\nDescription: " + txtDesc.Text;
            data += "\nQuantity: " + txtQuanti.Text;
            data += "\nUnit Price: " + txtPrice.Text;
            data += "\nAmout: " + txtAmount.Text;

            Graphics g = e.Graphics;
            g.DrawString(data, txtProName.Font, Brushes.Red, 20, 50);

        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            ppdPrintPreview.PrintPreviewControl.Zoom = 1;
            ppdPrintPreview.ShowDialog();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn chắc chắn muốn thoát trương trình?", "Exit program", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!validateInput())
            {
                return;
            }

            try
            {
                con = new SqlConnection(Properties.Resources.sCN);
                con.Open();
                string query = "UPDATE tbOrderBill SET Product=@2, Description=@3, Quantity=@4, UnitPrice=@5 WHERE BillNo = @1";
                SqlCommand cmd = new SqlCommand(query, con);

                cmd.Parameters.AddWithValue("@1", txtBillNo.Text.Trim());
                cmd.Parameters.AddWithValue("@2", txtProName.Text.Trim());
                cmd.Parameters.AddWithValue("@3", txtDesc.Text.Trim());
                cmd.Parameters.AddWithValue("@4", txtQuanti.Text.Trim());
                cmd.Parameters.AddWithValue("@5", txtPrice.Text.Trim());

                cmd.ExecuteNonQuery();
                MessageBox.Show("Update thành công!!!");

                ResetForm();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }

        private Boolean validateInput()
        {
            if ((txtBillNo.Text == "") || (txtBillNo.Text=="(Enter Bill No to Search or Update)"))
            {
                lblCheckBill.Visible = true;
                lblCheckBill.Text = "Bill No cannot be empty";
                txtBillNo.Focus();
                return false;
            }
            lblCheckBill.Visible = false;

            if (txtProName.Text == "")
            {
                lblCheckProName.Visible = true;
                lblCheckProName.Text = "Product Name cannot be empty";
                txtProName.Focus();
                return false;
            }
            lblCheckProName.Visible = false;

            if (txtDesc.Text == "")
            {
                lblCheckDesc.Visible = true;
                lblCheckDesc.Text = "Description cannot be empty";
                txtDesc.Focus();
                return false;
            }
            lblCheckDesc.Visible = false;

            if (txtQuanti.Text.Trim() == "")
            {
                lblCheckQuanti.Visible = true;
                lblCheckQuanti.Text = "Quantity cannot be empty";
                txtQuanti.Focus();
                return false;
            }
            lblCheckQuanti.Visible = false;

            if (txtPrice.Text.Trim() == "")
            {
                lblCheckPrice.Visible = true;
                lblCheckPrice.Text = "Description cannot be empty";
                txtPrice.Focus();
                return false;
            }
            lblCheckPrice.Visible = false;



            return true;
        }

        private void txtQuanti_KeyPress(object sender, KeyPressEventArgs e)
        {
            // Validate nhập liệu phải là số hoặc control
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
            
                
            
        }

        private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtQuanti_TextChanged(object sender, EventArgs e)
        {
            int quanti = 0;
            if (txtQuanti.Text == "")
            {
                quanti = 0;
            }
            else
            {
                quanti = int.Parse(txtQuanti.Text);
            }

            int price = int.Parse(txtPrice.Text);
            double amount = quanti * price;
            txtAmount.Text = "" + amount;
        }

        private void txtPrice_TextChanged(object sender, EventArgs e)
        {
            int price = 0;
            if (txtPrice.Text == "")
            {
                price = 0;
            }
            else
            {
                price = int.Parse(txtPrice.Text);
            }

            int quanti = int.Parse(txtQuanti.Text);
            double amount = quanti * price;
            txtAmount.Text = "" + amount;
        }
    }
}
